const express = require('express')
const sqlConn = require('../utils/sqlConn')
const { user } = require('../utils/sqlMap')
const { getRes, getToken } = require('../utils/tool')
const router = express.Router()

// 增加用户
router.post('/add', (req, res) => {
    const sqlStr = user.add
    const { username, phone, password, createTime } = req.body
    if (!username) {
        getRes(res, 0, '请输入用户名')
        return
    }
    if (!phone) {
        getRes(res, 0, '请输入phone')
        return
    }
    if (!password) {
        getRes(res, 0, '请输入密码')
        return
    }
    const conn = sqlConn()
    conn.query(sqlStr, [username, phone, password, createTime], (err, result) => {
        err ? getRes(res, 0, '新增用户失败', err) : getRes(res, 1, '新增用户成功', result)
    })
    conn.end()
})

// 查询用户，带分页
router.post('/query', (req, res) => {
    const sqlStr = user.query
    const { id, username, phone, startTime, endTime, currentPage, pageSize } = req.body
    const conn = sqlConn()
    const st = startTime || '0000-00-00'
    const et = endTime || new Date()
    const startSize = (currentPage - 1) * pageSize
    conn.query(sqlStr, [id, username, phone, st, et, startSize, pageSize], (err, result) => {
        const total = result && result.length ? result[0].total : 0
        err ? getRes(res, 0, '查询用户失败', err) : getRes(res, 1, '查询用户成功', { total, rows: result })
    })
    conn.end()
})

// 查询总条数


// 修改用户
router.post('/update', (req, res) => {
    const sqlStr = user.update
    const { username, phone, password, id } = req.body
    if (!username) {
        getRes(res, 0, '请输入用户名')
        return
    }
    if (!phone) {
        getRes(res, 0, '请输入phone')
        return
    }
    if (!password) {
        getRes(res, 0, '请输入密码')
        return
    }
    const conn = sqlConn()
    conn.query(sqlStr, [username, phone, password, id], (err, result) => {
        err ? getRes(res, 0, '修改用户失败', err) : getRes(res, 1, '修改用户成功', result)
    })
    conn.end()
})

// 删除用户
router.post('/delete', (req, res) => {
    const sqlStr = user.delete
    const { ids } = req.body
    if (!ids.length) {
        getRes(res, 0, '请选择用户')
        return
    }
    const conn = sqlConn()
    conn.query(sqlStr, [...ids], (err, result) => {
        err ? getRes(res, 0, '删除用户失败', err) : getRes(res, 1, '删除用户成功', result)
    })
    conn.end()
})

// 用户登录
router.post('/login', (req, res) => {
    const sqlStr = user.login
    const { username, password } = req.body
    if (!username) {
        getRes(res, 0, '请输入用户名')
        return
    }
    if (!password) {
        getRes(res, 0, '请输入密码')
        return
    }
    const conn = sqlConn()
    conn.query(sqlStr, [username, password], (err, result) => {
        if (result && result.length) {
            let item = result[0]
            const token = getToken.encrypt(item, 'token', '1h')
            item.token = token
            getRes(res, 1, '登录成功', item)
        } else {
            getRes(res, 0, '用户名或密码不正确，请重新输入', err)
        }
    })
    conn.end()
})

module.exports = router
